package com.wj.sort;

/**
 * 冒泡排序
 * @author wen.jie
 * @date 2021/8/4 17:10
 */
public class Bubble {

    /**
     * 排序
     * @author wen.jie
     * @date 2021/8/4 17:28
     */
    public static void sort(Comparable<?>[] a) {
        for (int i = a.length-1; i>0; i--) {
            for (int j = 0; j < i; j++) {
                if (greater(a[j], a[j+1])){
                    exchange(a, j, j+1);
                }
            }
        }
    }

    /**
     * 比较
     * @author wen.jie
     * @date 2021/8/4 17:18
     */
    private static boolean greater(Comparable v, Comparable w) {
        return v.compareTo(w) > 0;
    }

    /**
     * 交换
     * @author wen.jie
     * @date 2021/8/4 17:27
     */
    private static void exchange(Comparable[] a, int i, int j) {
        Comparable temp = a[i];
        a[i] = a[j];
        a[j] = temp;
    }

}
